package com.lant.config;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.DispatcherType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.lant.web.constant.UrlConstant;
import com.lant.web.interceptor.LoginHandlerInterceptor;

/**
 * @description: web工程配置
 * @projectName: lant-sso-web
 * @className: WebConfig
 * @author: YFL
 * @createTime: 2020年4月16日 下午6:47:22
 */
@MapperScan(value = "com.lant.dao")
@Configuration
@EnableEurekaClient
@EnableFeignClients
@PropertySource({"classpath:resource.properties"})
public class SSOConfig implements WebMvcConfigurer {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     * @Title: getloginHandlerInterceptor
     * @Description: bean提前加载拦截器
     * @return: LoginHandlerInterceptor
     * @author: YFL
     * @date: 2020年5月25日 下午1:58:56
     */
    @Bean
    public LoginHandlerInterceptor getloginHandlerInterceptor() {
        return new LoginHandlerInterceptor();
    }

    /**
     * @Title: addInterceptors
     * @Description: 在springboot2.0.0之后继承WebMvcConfigurationSupport类，重写addInterceptors方法
     * @param: registry
     * @author: YFL
     * @date: 2020年5月21日 下午7:29:51
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginHandlerInterceptor loginHandlerInterceptor = this.getloginHandlerInterceptor();
        loginHandlerInterceptor.setInherentEscapeURIS(this.authorizeUrl());
        /**
         * addInterceptor 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL，前面的拦截器会执行，后面的拦截器将不执行
         */
        registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/**").excludePathPatterns("/webjars/**",
            "/layui/**", "/iview/**", "/js/**", "/css/**", "/img/**");
    }

    /**
     * @Title: authorizeUrl
     * @Description: 不需要验证登录页面
     * @return: Set<String>
     * @author: YFL
     * @date: 2020年5月21日 下午7:30:08
     */
    private Set<String> authorizeUrl() {
        Set<String> excludeURIs = new HashSet<>();
        excludeURIs.add(UrlConstant.GET_VERIFY_URL); // 登录验证码
        excludeURIs.add(UrlConstant.CHECK_VERIFY_URL); // 校验验证码
        excludeURIs.add(UrlConstant.GET_SMS_VERIFY_URL); // 获取短信验证码
        excludeURIs.add(UrlConstant.CHECK_SMS_VERIFY_URL); // 校验短信验证码
        excludeURIs.add(UrlConstant.PAGE_LOGIN_URL); // 登录页，需要拦截，直接token登录
        excludeURIs.add(UrlConstant.PAGE_REGISTER_URL); // 用户注册页面
        excludeURIs.add(UrlConstant.USER_LOGIN_URL); // 用户登录
        excludeURIs.add(UrlConstant.USER_LOGOUT_URL); // 退出登录,清空session
        excludeURIs.add(UrlConstant.USER_SAVE_URL); // 用户注册
        excludeURIs.add(UrlConstant.USER_CHECK_DATA_URL); // 检测用户数据
        return excludeURIs;
    }

    /**
     * encodingFilter 编码的统一处理
     */
    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> encodingFilter() {
        FilterRegistrationBean<CharacterEncodingFilter> filterBean = new FilterRegistrationBean<>();
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);

        filterBean.setFilter(filter);
        EnumSet<DispatcherType> dispatcherTypes = EnumSet.allOf(DispatcherType.class);
        dispatcherTypes.add(DispatcherType.REQUEST);
        dispatcherTypes.add(DispatcherType.FORWARD);
        filterBean.setDispatcherTypes(dispatcherTypes);
        filterBean.setEnabled(true);
        filterBean.addUrlPatterns("/*");
        return filterBean;
    }
}
